// pages/messageList/messageList.js
const app = getApp()
const timeFormatting = require('../../utils/timeFormatting.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNumber: 1,
    totalPages: '',
  },
  getlist() {
    var that = this
    app.req({
      url: 'blessmini/user/list',
      data: {
        pageNumber: that.data.pageNumber,
        pageSize: 10
      },
      success: function(res) {
        if (200 == res.code) {
          var nowList = res.data;
          for (var i in nowList) {
            var item = nowList[i];
            if (item.createTime) {
              var time = new Date((item.createTime).replace(/-/g, "/")).getTime();
              item.createTime = timeFormatting.showDate(new Date(time))
            }
          }
          if(that.data.pageNumber==1){
            that.toggle("list",nowList);
          }else{
            that.toggle("list", that.data.list.concat(nowList));
          }
          that.toggle("totalPages", res.data.totalPages);
        }
      }
    });
  },
  allread() {
    var that = this;
    app.req({
      url: "blessmini/user/updateByType",
      method: "post",
      data: {
      },
      success: function(res) {
        if (200 == res.code) {
          that.getlist();
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    });
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  topage(e){
    var that = this
    if(e.currentTarget.dataset.type != 3){
      app.req({
        url: "blessmini/user/readMessage",
        method: "post",
        data: {
          id: e.currentTarget.dataset.msgid
        },
        success: function (res) {
          if (200 == res.code) {
            that.getlist();
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      });
    }
    if (e.currentTarget.dataset.type==1){
      wx.navigateTo({
        url: '/pages/ordinaryDetails/ordinaryDetails?id=' + e.currentTarget.dataset.id,
      })
    }else if(e.currentTarget.dataset.type==3){
      wx.navigateTo({
        url: '/pages/myMoneyRecord/myMoneyRecord'
      })
    }else{
      wx.navigateTo({
        url: '/pages/hundredDetailed/hundredDetailed?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    that.toggle("list", []);
    that.toggle("pageNumber", 1);
    that.getlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (that.data.totalPages > that.data.pageNumber) {
      that.setData({
        pageNumber: that.data.pageNumber + 1
      })
      that.getlist()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})